using DAO;
using Easislides.Properties;
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace Easislides
{
	public class FrmExport : Form
	{
		private IContainer components = null;
		private CheckedListBox FolderList;
		private Button BtnCancel;
		private Button BtnOK;
		private CheckBox cbSongsListTickAll;
		private Label tbExportTo;
		private ProgressBar ProgressBar1;
		private GroupBox groupBox5;
		private RadioButton OptExport1;
		private RadioButton OptExport0;
		private Panel panelLinkTitle2Lookup;
		private ToolStrip toolStrip2;
		private ToolStripButton Export_FileName;
		private GroupBox groupBox1;
		private GroupBox groupBox2;
		private DateTimePicker CalendarFrom;
		private Label label2;
		private DateTimePicker CalendarTo;
		private Label label1;
		private CheckBox cbFolderListTickAll;
		private ListView SongsList;
		private ColumnHeader columnHeader1;
		private ColumnHeader columnHeader4;
		private SaveFileDialog saveFileDialog1;
		private ColumnHeader columnHeader3;
		private int sortColumn = -1;
		private string SongID1;
		private string OutputFileName;
		private string SongHeader;
		private string tempSequence;
		private string UserReference;
		private string BookReference;
		private string SongNumber;
		private string SongCopyright;
		private string SongWriterInfo;
		private string SongLyrics;
		private string SongFolderNo;
		private string SongTitle2;
		private string SongTitle;
		private string SongTiming;
		private string SongCapo;
		private string SongKey;
		private string Lic_Admin2;
		private string Lic_Admin1;
		private int TotSongsSel;
		private string InContents;
		private bool GroupCheck = false;
		private string DefaultFileName = "ESExport_";
		private gf.SongSettings ExportItem = new gf.SongSettings();
		private string Preferred_Ext = "";
		private bool GeneratingList = false;
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
            this.FolderList = new System.Windows.Forms.CheckedListBox();
            this.BtnCancel = new System.Windows.Forms.Button();
            this.BtnOK = new System.Windows.Forms.Button();
            this.cbSongsListTickAll = new System.Windows.Forms.CheckBox();
            this.tbExportTo = new System.Windows.Forms.Label();
            this.ProgressBar1 = new System.Windows.Forms.ProgressBar();
            this.groupBox5 = new System.Windows.Forms.GroupBox();
            this.CalendarTo = new System.Windows.Forms.DateTimePicker();
            this.CalendarFrom = new System.Windows.Forms.DateTimePicker();
            this.OptExport1 = new System.Windows.Forms.RadioButton();
            this.OptExport0 = new System.Windows.Forms.RadioButton();
            this.label2 = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.panelLinkTitle2Lookup = new System.Windows.Forms.Panel();
            this.toolStrip2 = new System.Windows.Forms.ToolStrip();
            this.Export_FileName = new System.Windows.Forms.ToolStripButton();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.cbFolderListTickAll = new System.Windows.Forms.CheckBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.SongsList = new System.Windows.Forms.ListView();
            this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.columnHeader4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            this.groupBox5.SuspendLayout();
            this.panelLinkTitle2Lookup.SuspendLayout();
            this.toolStrip2.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // FolderList
            // 
            this.FolderList.CheckOnClick = true;
            this.FolderList.FormattingEnabled = true;
            this.FolderList.Location = new System.Drawing.Point(8, 18);
            this.FolderList.Name = "FolderList";
            this.FolderList.Size = new System.Drawing.Size(184, 109);
            this.FolderList.TabIndex = 0;
            this.FolderList.SelectedValueChanged += new System.EventHandler(this.FolderList_SelectedValueChanged);
            // 
            // BtnCancel
            // 
            this.BtnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.BtnCancel.Location = new System.Drawing.Point(462, 339);
            this.BtnCancel.Name = "BtnCancel";
            this.BtnCancel.Size = new System.Drawing.Size(80, 24);
            this.BtnCancel.TabIndex = 6;
            this.BtnCancel.Text = "Close";
            this.BtnCancel.Click += new System.EventHandler(this.BtnCancel_Click);
            // 
            // BtnOK
            // 
            this.BtnOK.Location = new System.Drawing.Point(366, 339);
            this.BtnOK.Name = "BtnOK";
            this.BtnOK.Size = new System.Drawing.Size(80, 24);
            this.BtnOK.TabIndex = 5;
            this.BtnOK.Text = "Export";
            this.BtnOK.Click += new System.EventHandler(this.BtnOK_Click);
            // 
            // cbSongsListTickAll
            // 
            this.cbSongsListTickAll.AutoSize = true;
            this.cbSongsListTickAll.Location = new System.Drawing.Point(14, 267);
            this.cbSongsListTickAll.Name = "cbSongsListTickAll";
            this.cbSongsListTickAll.Size = new System.Drawing.Size(61, 17);
            this.cbSongsListTickAll.TabIndex = 1;
            this.cbSongsListTickAll.Text = "Tick All";
            this.cbSongsListTickAll.ThreeState = true;
            this.cbSongsListTickAll.CheckedChanged += new System.EventHandler(this.cbSongsListTickAll_CheckedChanged);
            // 
            // tbExportTo
            // 
            this.tbExportTo.BackColor = System.Drawing.SystemColors.Control;
            this.tbExportTo.Location = new System.Drawing.Point(19, 316);
            this.tbExportTo.Name = "tbExportTo";
            this.tbExportTo.Size = new System.Drawing.Size(480, 13);
            this.tbExportTo.TabIndex = 4;
            // 
            // ProgressBar1
            // 
            this.ProgressBar1.Location = new System.Drawing.Point(13, 312);
            this.ProgressBar1.Name = "ProgressBar1";
            this.ProgressBar1.Size = new System.Drawing.Size(495, 21);
            this.ProgressBar1.Step = 1;
            this.ProgressBar1.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
            this.ProgressBar1.TabIndex = 3;
            // 
            // groupBox5
            // 
            this.groupBox5.Controls.Add(this.CalendarTo);
            this.groupBox5.Controls.Add(this.CalendarFrom);
            this.groupBox5.Controls.Add(this.OptExport1);
            this.groupBox5.Controls.Add(this.OptExport0);
            this.groupBox5.Controls.Add(this.label2);
            this.groupBox5.Controls.Add(this.label1);
            this.groupBox5.Location = new System.Drawing.Point(12, 182);
            this.groupBox5.Name = "groupBox5";
            this.groupBox5.Size = new System.Drawing.Size(198, 120);
            this.groupBox5.TabIndex = 1;
            this.groupBox5.TabStop = false;
            this.groupBox5.Text = "List Items from Selected Folders";
            // 
            // CalendarTo
            // 
            this.CalendarTo.Location = new System.Drawing.Point(38, 91);
            this.CalendarTo.Name = "CalendarTo";
            this.CalendarTo.Size = new System.Drawing.Size(156, 20);
            this.CalendarTo.TabIndex = 5;
            this.CalendarTo.ValueChanged += new System.EventHandler(this.Calendar_ValueChanged);
            // 
            // CalendarFrom
            // 
            this.CalendarFrom.Location = new System.Drawing.Point(38, 64);
            this.CalendarFrom.Name = "CalendarFrom";
            this.CalendarFrom.Size = new System.Drawing.Size(156, 20);
            this.CalendarFrom.TabIndex = 3;
            this.CalendarFrom.ValueChanged += new System.EventHandler(this.Calendar_ValueChanged);
            // 
            // OptExport1
            // 
            this.OptExport1.AutoSize = true;
            this.OptExport1.Location = new System.Drawing.Point(8, 41);
            this.OptExport1.Name = "OptExport1";
            this.OptExport1.Size = new System.Drawing.Size(133, 17);
            this.OptExport1.TabIndex = 1;
            this.OptExport1.Text = "Items Added/Updated:";
            // 
            // OptExport0
            // 
            this.OptExport0.AutoSize = true;
            this.OptExport0.Location = new System.Drawing.Point(8, 18);
            this.OptExport0.Name = "OptExport0";
            this.OptExport0.Size = new System.Drawing.Size(64, 17);
            this.OptExport0.TabIndex = 0;
            this.OptExport0.Text = "All Items";
            this.OptExport0.CheckedChanged += new System.EventHandler(this.OptExport_CheckedChanged);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(7, 95);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(23, 13);
            this.label2.TabIndex = 4;
            this.label2.Text = "To:";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(6, 68);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(33, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "From:";
            // 
            // panelLinkTitle2Lookup
            // 
            this.panelLinkTitle2Lookup.Controls.Add(this.toolStrip2);
            this.panelLinkTitle2Lookup.Location = new System.Drawing.Point(514, 311);
            this.panelLinkTitle2Lookup.Name = "panelLinkTitle2Lookup";
            this.panelLinkTitle2Lookup.Size = new System.Drawing.Size(22, 22);
            this.panelLinkTitle2Lookup.TabIndex = 48;
            // 
            // toolStrip2
            // 
            this.toolStrip2.AutoSize = false;
            this.toolStrip2.CanOverflow = false;
            this.toolStrip2.Dock = System.Windows.Forms.DockStyle.None;
            this.toolStrip2.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.toolStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.Export_FileName});
            this.toolStrip2.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Flow;
            this.toolStrip2.Location = new System.Drawing.Point(0, 0);
            this.toolStrip2.Name = "toolStrip2";
            this.toolStrip2.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
            this.toolStrip2.Size = new System.Drawing.Size(25, 28);
            this.toolStrip2.TabIndex = 0;
            // 
            // Export_FileName
            // 
            this.Export_FileName.AutoSize = false;
            this.Export_FileName.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.Export_FileName.Image = global::Properties.Resources.open;
            this.Export_FileName.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.Export_FileName.Name = "Export_FileName";
            this.Export_FileName.Size = new System.Drawing.Size(22, 22);
            this.Export_FileName.Tag = "";
            this.Export_FileName.ToolTipText = "Export file name";
            this.Export_FileName.Click += new System.EventHandler(this.Export_FileName_Click);
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.cbFolderListTickAll);
            this.groupBox1.Controls.Add(this.FolderList);
            this.groupBox1.Location = new System.Drawing.Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(198, 164);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Select Song Folder to Export";
            // 
            // cbFolderListTickAll
            // 
            this.cbFolderListTickAll.AutoSize = true;
            this.cbFolderListTickAll.Location = new System.Drawing.Point(9, 133);
            this.cbFolderListTickAll.Name = "cbFolderListTickAll";
            this.cbFolderListTickAll.Size = new System.Drawing.Size(61, 17);
            this.cbFolderListTickAll.TabIndex = 1;
            this.cbFolderListTickAll.Text = "Tick All";
            this.cbFolderListTickAll.ThreeState = true;
            this.cbFolderListTickAll.CheckedChanged += new System.EventHandler(this.cbFolderListTickAll_CheckedChanged);
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.SongsList);
            this.groupBox2.Controls.Add(this.cbSongsListTickAll);
            this.groupBox2.Location = new System.Drawing.Point(216, 12);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(326, 290);
            this.groupBox2.TabIndex = 2;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Items Found:";
            // 
            // SongsList
            // 
            this.SongsList.CheckBoxes = true;
            this.SongsList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1,
            this.columnHeader3,
            this.columnHeader4});
            this.SongsList.FullRowSelect = true;
            this.SongsList.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
            this.SongsList.Location = new System.Drawing.Point(6, 19);
            this.SongsList.Name = "SongsList";
            this.SongsList.ShowItemToolTips = true;
            this.SongsList.Size = new System.Drawing.Size(314, 242);
            this.SongsList.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.SongsList.TabIndex = 0;
            this.SongsList.UseCompatibleStateImageBehavior = false;
            this.SongsList.View = System.Windows.Forms.View.Details;
            this.SongsList.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(this.SongsList_ItemChecked);
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "Tick the items you wish to Export";
            this.columnHeader1.Width = 192;
            // 
            // columnHeader3
            // 
            this.columnHeader3.DisplayIndex = 2;
            this.columnHeader3.Text = "ID";
            this.columnHeader3.Width = 0;
            // 
            // columnHeader4
            // 
            this.columnHeader4.DisplayIndex = 1;
            this.columnHeader4.Text = "Song Folder";
            this.columnHeader4.Width = 96;
            // 
            // FrmExport
            // 
            this.AcceptButton = this.BtnOK;
            this.CancelButton = this.BtnCancel;
            this.ClientSize = new System.Drawing.Size(548, 377);
            this.Controls.Add(this.tbExportTo);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.panelLinkTitle2Lookup);
            this.Controls.Add(this.groupBox5);
            this.Controls.Add(this.BtnCancel);
            this.Controls.Add(this.BtnOK);
            this.Controls.Add(this.ProgressBar1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FrmExport";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Export";
            this.Load += new System.EventHandler(this.FrmExport_Load);
            this.groupBox5.ResumeLayout(false);
            this.groupBox5.PerformLayout();
            this.panelLinkTitle2Lookup.ResumeLayout(false);
            this.toolStrip2.ResumeLayout(false);
            this.toolStrip2.PerformLayout();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.ResumeLayout(false);

		}
		public FrmExport()
		{
			this.InitializeComponent();
		}
		private void FrmExport_Load(object sender, EventArgs e)
		{
			this.OptExport0.Checked = true;
			this.CalendarFrom.Value = DateTime.Now.Subtract(TimeSpan.FromDays(91.0));
			this.BuildFolderList();
			this.ExportItem.Initialise();
			this.SongsList.Sorting = SortOrder.None;
			this.Preferred_Ext = ".xml";
			this.tbExportTo.Text = ((gf.ExportFileName != "") ? gf.ExportFileName : this.GetLowestFileNum(gf.DocumentsDir + "Export_" + DateTime.Today.ToString("yyyy-MM-dd"), this.Preferred_Ext));
		}
		private void BuildFolderList()
		{
			this.FolderList.Items.Clear();
			for (int i = 1; i < 41; i++)
			{
				if (gf.FolderUse[i] > 0)
				{
					this.FolderList.Items.Add(gf.FolderName[i]);
				}
			}
			if (this.FolderList.Items.Count == 0)
			{
				this.FolderList.Items.Add(gf.FolderName[1]);
			}
			this.FolderList.Text = this.FolderList.Items[0].ToString();
			this.SongsList.Items.Clear();
			this.DisplayCount();
		}
		private string ValidateExportExtension(string InExtension)
		{
			InExtension = InExtension.ToLower();
			return (InExtension == ".esn" || InExtension == ".xml") ? InExtension : ".xml";
		}
		private void CreateExportList()
		{
			this.GeneratingList = true;
			this.SongsList.Items.Clear();
			this.SongsList.Sorting = SortOrder.None;
			if (this.FolderList.CheckedItems.Count >= 1)
			{
				ListViewItem listViewItem = new ListViewItem();
				string str;
				if (this.OptExport1.Checked)
				{
					string text = this.CalendarFrom.Value.ToString("MM-dd-yyyy");
					string text2 = this.CalendarTo.Value.ToString("MM-dd-yyyy");
					str = string.Concat(new string[]
					{
						" and LastModified >=#",
						text,
						"# and LastModified <=#",
						text2,
						"#"
					});
				}
				else
				{
					str = "";
				}
				string text3 = "";
				for (int i = 0; i < this.FolderList.CheckedItems.Count; i++)
				{
					if (text3 == "")
					{
						text3 = "select * from SONG where (FolderNo=" + Convert.ToString(gf.GetFolderNumber(this.FolderList.CheckedItems[i].ToString()));
					}
					else
					{
						text3 = text3 + " or FolderNo=" + Convert.ToString(gf.GetFolderNumber(this.FolderList.CheckedItems[i].ToString()));
					}
				}
				text3 = text3 + ") " + str + " order by cjk_strokecount";
				int num = 0;
				try
				{
					this.Cursor = Cursors.WaitCursor;
					Recordset recordSet = gf.GetRecordSet(gf.ConnectStringMainDB, text3);
					if (recordSet != null && !recordSet.EOF)
					{
						recordSet.MoveFirst();
						while (!recordSet.EOF)
						{
							this.SongID1 = gf.ObjToString(recordSet.Fields["SongID"].Value);
							this.SongTitle = gf.ObjToString(recordSet.Fields["Title_1"].Value);
							int num2 = gf.ObjToInt(recordSet.Fields["FolderNo"].Value);
							listViewItem = this.SongsList.Items.Add(this.SongTitle);
							listViewItem.SubItems.Add(this.SongID1);
							listViewItem.SubItems.Add(gf.FolderName[num2]);
							num++;
							recordSet.MoveNext();
						}
					}
					recordSet.Close();
					if (recordSet != null)
					{
					}
				}
				catch
				{
				}
				this.SongsList.Sorting = SortOrder.Ascending;
				this.SongsList.Sort();
				this.DisplayCount();
				this.GeneratingList = false;
				this.Cursor = Cursors.Default;
			}
		}
		private void DisplayCount()
		{
			if (!this.GroupCheck)
			{
				int count = this.SongsList.Items.Count;
				if (count < 1)
				{
					this.SongsList.Columns[0].Text = "No Items found";
				}
				else
				{
					int count2 = this.SongsList.CheckedItems.Count;
					this.SongsList.Columns[0].Text = string.Concat(new string[]
					{
						" ",
						Convert.ToString(count),
						" items found : ",
						Convert.ToString(count2),
						" ticked."
					});
				}
			}
		}
		private void FolderList_SelectedValueChanged(object sender, EventArgs e)
		{
			this.FolderListCheckedItems_Changed();
		}
		private void FolderListCheckedItems_Changed()
		{
			if (this.FolderList.CheckedItems.Count <= 0)
			{
				this.SongsList.Items.Clear();
				this.cbFolderListTickAll.CheckState = CheckState.Unchecked;
				this.cbSongsListTickAll.CheckState = CheckState.Unchecked;
			}
			this.CreateExportList();
		}
		private void SongsList_ItemChecked(object sender, ItemCheckedEventArgs e)
		{
			if (!this.GeneratingList)
			{
				this.DisplayCount();
			}
		}
		private string GetLowestFileNum(string InFileName, string InExt)
		{
			string result;
			if (!File.Exists(InFileName + InExt))
			{
				result = InFileName + InExt;
			}
			else
			{
				int num = 0;
				for (int i = 98; i >= 1; i--)
				{
					if (File.Exists(InFileName + "_" + i.ToString("00") + InExt))
					{
						num = i;
						i = 0;
					}
				}
				num++;
				if (num == 99 & !File.Exists(InFileName + "_" + num.ToString("00") + InExt))
				{
					for (int i = 1; i < 100; i++)
					{
						if (!File.Exists(InFileName + "_" + i.ToString("00") + InExt))
						{
							num = i;
							i = 100;
						}
					}
				}
				result = InFileName + "_" + num.ToString("00") + InExt;
			}
			return result;
		}
		private void cbSongsListTickAll_CheckedChanged(object sender, EventArgs e)
		{
			this.SetSongsListCheckBoxes();
		}
		private void SetSongsListCheckBoxes()
		{
			if (this.SongsList.Items.Count > 0)
			{
				this.GeneratingList = true;
				this.Cursor = Cursors.WaitCursor;
				if (this.cbSongsListTickAll.CheckState == CheckState.Checked)
				{
					for (int i = this.SongsList.Items.Count - 1; i >= 0; i--)
					{
						this.SongsList.Items[i].Checked = true;
					}
				}
				else
				{
					if (this.cbSongsListTickAll.CheckState == CheckState.Unchecked)
					{
						for (int i = this.SongsList.Items.Count - 1; i >= 0; i--)
						{
							this.SongsList.Items[i].Checked = false;
						}
					}
				}
				this.DisplayCount();
				this.GeneratingList = false;
				this.Cursor = Cursors.Default;
			}
		}
		private void cbFolderListTickAll_CheckedChanged(object sender, EventArgs e)
		{
			this.SetFolderListCheckBoxes();
		}
		private void SetFolderListCheckBoxes()
		{
			if (this.FolderList.Items.Count != 0)
			{
				if (this.cbFolderListTickAll.CheckState == CheckState.Checked)
				{
					for (int i = this.FolderList.Items.Count - 1; i >= 0; i--)
					{
						this.FolderList.SetItemChecked(i, true);
					}
				}
				else
				{
					if (this.cbFolderListTickAll.CheckState == CheckState.Unchecked)
					{
						for (int i = this.FolderList.Items.Count - 1; i >= 0; i--)
						{
							this.FolderList.SetItemChecked(i, false);
						}
					}
				}
				this.FolderListCheckedItems_Changed();
			}
		}
		private void OptExport_CheckedChanged(object sender, EventArgs e)
		{
			this.CalendarFrom.Enabled = this.OptExport1.Checked;
			this.CalendarTo.Enabled = this.CalendarFrom.Enabled;
			this.CreateExportList();
		}
		private void Calendar_ValueChanged(object sender, EventArgs e)
		{
			this.CreateExportList();
		}
		private void Export_FileName_Click(object sender, EventArgs e)
		{
			this.saveFileDialog1.Filter = "EasiSlides XML File (*.xml)|*.xml|EasiSlides Database File (*.esf)|*.esf|EasiSlides Text File (*.esn)|*.esn";
			this.saveFileDialog1.InitialDirectory = gf.ExportToDir;
			string text = this.tbExportTo.Text;
			this.saveFileDialog1.FileName = gf.GetDisplayNameOnly(ref text, false, false);
			this.saveFileDialog1.OverwritePrompt = false;
			this.saveFileDialog1.DefaultExt = this.Preferred_Ext;
			if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				string extension = Path.GetExtension(this.saveFileDialog1.FileName);
				string str = gf.Left(this.saveFileDialog1.FileName, this.saveFileDialog1.FileName.Length - extension.Length);
				this.tbExportTo.Text = str + extension;
				gf.ExportFileName = this.tbExportTo.Text;
				gf.ExportToDir = Path.GetDirectoryName(this.tbExportTo.Text) + "\\";
				string extension2 = Path.GetExtension(this.tbExportTo.Text);
				this.Preferred_Ext = this.ValidateExportExtension(extension2);
				gf.SaveRegValue("settings", "export_ext", this.Preferred_Ext);
			}
		}
		private void BtnOK_Click(object sender, EventArgs e)
		{
			this.Start_Export();
		}
		private void Start_Export()
		{
			if (this.FolderList.CheckedItems.Count == 0)
			{
				MessageBox.Show("You have not selected any folders to export!");
			}
			else
			{
				if (this.SongsList.Items.Count < 1)
				{
					MessageBox.Show("No Songs exported - the Songs List is empty!");
				}
				else
				{
					this.TotSongsSel = this.SongsList.CheckedItems.Count;
					if (this.TotSongsSel < 1)
					{
						MessageBox.Show("Please Tick the songs you wish to export");
					}
					else
					{
						string text = this.tbExportTo.Text;
						if (File.Exists(text))
						{
							if (MessageBox.Show("This will overwrite the existing document '" + text + "' - OK to Continue?", "Overwrite", MessageBoxButtons.OKCancel) != DialogResult.OK)
							{
								MessageBox.Show("Export file NOT produced.");
								return;
							}
							try
							{
								File.Delete(text);
							}
							catch
							{
								MessageBox.Show("For some reason, Windows could not create the file '" + text + "'. Export NOT done.");
								this.ProgressBar1.Value = 0;
								return;
							}
						}
						this.tbExportTo.Visible = false;
						if (gf.Right(text, 4) == ".esf")
						{
							this.Export_DatabaseFormat(text);
						}
						if (gf.Right(text, 4) == ".esn")
						{
							this.Export_TextFormat(text);
						}
						else
						{
							this.Export_XMLFormat(text);
						}
						this.tbExportTo.Visible = true;
					}
				}
			}
		}
		private void Export_DatabaseFormat(string ExportFileName)
		{
			string text = Application.StartupPath + "\\Sys\\Defdb.dat";
			if (File.Exists(text))
			{
				gf.ValidateDir(Path.GetDirectoryName(ExportFileName) + "\\", true);
				File.Copy(text, ExportFileName, true);
				Recordset tableRecordSet = gf.GetTableRecordSet(gf.ConnectStringMainDB, "SONG");
				tableRecordSet.Index = "PrimaryKey";
				this.Cursor = Cursors.WaitCursor;
				this.ProgressBar1.Visible = true;
				this.ProgressBar1.Value = 0;
				int num = 0;
				if (gf.DeleteAllFolders(gf.ConnectStringDef + ExportFileName))
				{
					for (int i = 0; i < this.FolderList.CheckedItems.Count; i++)
					{
						gf.ResetFolder(gf.GetFolderNumber(this.FolderList.CheckedItems[i].ToString()), this.FolderList.CheckedItems[i].ToString(), gf.ConnectStringDef + ExportFileName);
					}
					for (int i = 0; i < this.SongsList.Items.Count; i++)
					{
						if (this.SongsList.Items[i].Checked)
						{
							if (gf.LoadDataIntoItem(ref this.ExportItem, tableRecordSet, this.SongsList.Items[i].SubItems[1].Text))
							{
								num = gf.InsertItemIntoDatabase(gf.ConnectStringDef + ExportFileName, this.ExportItem);
							}
							base.Update();
							int num2 = (i + 1) * 100 / this.TotSongsSel;
							this.ProgressBar1.Value = ((num2 > 100) ? 100 : num2);
							this.ProgressBar1.Invalidate();
							if (num < 1)
							{
								i = this.SongsList.Items.Count;
							}
						}
					}
					this.Cursor = Cursors.Default;
					if (num > 0)
					{
						MessageBox.Show("Export Completed. Total of " + Convert.ToString(this.SongsList.CheckedItems.Count) + " songs exported to " + ExportFileName);
					}
					else
					{
						MessageBox.Show("Error encountered when exporting to database file'" + ExportFileName + "'. Export NOT completed.");
					}
				}
				else
				{
					MessageBox.Show("Error encountered when trying to create export database file'" + ExportFileName + "'. Export NOT completed.");
				}
				if (tableRecordSet != null)
				{
				}
				this.ProgressBar1.Value = 0;
				this.Cursor = Cursors.Default;
			}
			else
			{
				MessageBox.Show("System Error: cannot create export database file. You may need to re-install EasiSlides Software.");
			}
		}
		private void Export_TextFormat(string ExportFileName)
		{
			int num = 0;
			gf.ValidateDir(Path.GetDirectoryName(ExportFileName), true);
			this.ProgressBar1.Visible = true;
			this.ProgressBar1.Value = 0;
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("[est3.1]");
			Recordset recordset = gf.GetTableRecordSet(gf.ConnectStringMainDB, "SONG");
			recordset.Index = "PrimaryKey";
			this.Cursor = Cursors.WaitCursor;
			for (int i = 0; i < this.SongsList.Items.Count; i++)
			{
				if (this.SongsList.Items[i].Checked && gf.LoadDataIntoItem(ref this.ExportItem, recordset, this.SongsList.Items[i].SubItems[1].Text))
				{
					stringBuilder.Append("\r\n[>" + this.ExportItem.Title);
					if (this.ExportItem.Title2 != "")
					{
						stringBuilder.Append(">>" + this.ExportItem.Title2);
					}
					if (this.ExportItem.FolderNo > 0)
					{
						stringBuilder.Append(">f" + gf.FolderName[this.ExportItem.FolderNo]);
					}
					if (this.ExportItem.SongNumber > 0)
					{
						stringBuilder.Append(">n" + this.ExportItem.SongNumber);
					}
					if (this.ExportItem.Book_Reference != "")
					{
						stringBuilder.Append(">r" + this.ExportItem.Book_Reference);
					}
					if (this.ExportItem.User_Reference != "")
					{
						stringBuilder.Append(">u" + this.ExportItem.User_Reference);
					}
					if (this.ExportItem.Copyright != "")
					{
						stringBuilder.Append(">c" + this.ExportItem.Copyright);
					}
					if (this.ExportItem.Writer != "")
					{
						stringBuilder.Append(">w" + this.ExportItem.Writer);
					}
					if (this.ExportItem.MusicKey != "")
					{
						stringBuilder.Append(">k" + this.ExportItem.MusicKey);
					}
					if (this.ExportItem.Timing != "")
					{
						stringBuilder.Append(">t" + this.ExportItem.Timing);
					}
					if (this.ExportItem.Capo >= 0)
					{
						stringBuilder.Append(">0" + this.ExportItem.Capo);
					}
					if (this.ExportItem.Show_LicAdminInfo1 != "")
					{
						stringBuilder.Append(">a" + this.ExportItem.Show_LicAdminInfo1);
					}
					if (this.ExportItem.Show_LicAdminInfo2 != "")
					{
						stringBuilder.Append(">b" + this.ExportItem.Show_LicAdminInfo2);
					}
					if (this.ExportItem.SongSequence.Length > 0)
					{
						this.tempSequence = "";
						for (int j = 0; j < this.ExportItem.SongSequence.Length; j++)
						{
							int num2 = gf.StringToInt(this.ExportItem.SongSequence[j]);
							if (num2 > 0 & num2 < 13)
							{
								this.tempSequence += Convert.ToString(num2);
							}
							else
							{
								this.tempSequence += gf.SequenceSymbol[num2];
							}
							if (j < this.ExportItem.SongSequence.Length - 1)
							{
								this.tempSequence += ",";
							}
						}
						stringBuilder.Append(">@" + this.tempSequence);
					}
					stringBuilder.Append("]");
					if (this.ExportItem.Notations != "")
					{
						string str = "[~" + this.ExportItem.Notations + "]";
						stringBuilder.Append("\r\n" + str);
					}
					this.ExportItem.CompleteLyrics.Replace("\r\n", "\n");
					stringBuilder.Append("\r\n" + this.ExportItem.CompleteLyrics.Replace("\n", "\r\n"));
					num++;
					base.Update();
					int num3 = num * 100 / this.TotSongsSel;
					this.ProgressBar1.Value = ((num3 > 100) ? 100 : num3);
					this.ProgressBar1.Invalidate();
				}
			}
			recordset = null;
			if (recordset != null)
			{
			}
			if (gf.CreateNewFile(ExportFileName, gf.FileContentsType.DoubleByte, stringBuilder.ToString()))
			{
				MessageBox.Show("Export Completed. Total of " + Convert.ToString(num) + " songs exported to " + ExportFileName);
			}
			else
			{
				MessageBox.Show("Error encountered when trying to create export file'" + ExportFileName + "'. Export NOT completed.");
			}
			this.Cursor = Cursors.Default;
			this.ProgressBar1.Visible = false;
		}
		private void Export_XMLFormat(string ExportFileName)
		{
			gf.ValidateDir(Path.GetDirectoryName(ExportFileName) + "\\", true);
			Recordset tableRecordSet = gf.GetTableRecordSet(gf.ConnectStringMainDB, "SONG");
			tableRecordSet.Index = "PrimaryKey";
			this.Cursor = Cursors.WaitCursor;
			this.ProgressBar1.Visible = true;
			this.ProgressBar1.Value = 0;
			int num = 0;
			try
			{
				XmlTextWriter xmlTextWriter = new XmlTextWriter(ExportFileName, Encoding.UTF8);
				xmlTextWriter.Formatting = Formatting.Indented;
				xmlTextWriter.WriteStartDocument();
				xmlTextWriter.WriteStartElement("EasiSlides");
				for (int i = 0; i < this.SongsList.Items.Count; i++)
				{
					if (this.SongsList.Items[i].Checked)
					{
						if (gf.LoadDataIntoItem(ref this.ExportItem, tableRecordSet, this.SongsList.Items[i].SubItems[1].Text))
						{
							gf.WriteXMLOneItem(ref xmlTextWriter, this.ExportItem, null, false);
							num++;
						}
						base.Update();
						int num2 = (i + 1) * 100 / this.TotSongsSel;
						this.ProgressBar1.Value = ((num2 > 100) ? 100 : num2);
						this.ProgressBar1.Invalidate();
					}
				}
				xmlTextWriter.WriteEndDocument();
				xmlTextWriter.Flush();
				xmlTextWriter.Close();
				this.Cursor = Cursors.Default;
				MessageBox.Show("Export Completed. Total of " + Convert.ToString(num) + " songs exported to " + ExportFileName);
			}
			catch
			{
				this.Cursor = Cursors.Default;
				MessageBox.Show("Error encountered when trying to create export file'" + ExportFileName + "'. Export NOT completed.");
			}
			this.ProgressBar1.Value = 0;
		}

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
	}
}
